/**
 * Created by duwei on 2019/4/4.
 */
var ZiPaiRoomErQiShi = ZiPaiJavaRoom.extend({
    _ccsFileName: zipai.res.ZiPaiRoomErQiShi_json,
    isBaiPai:false,
    isPaiOffSetY:true,
    /**
     * 吃碰后保留自己的摆牌
     */
    chiPengCardDeal:true,
    initLog: function () {
        var self = this;
        var game_title = self.$('RoomInfo.game_title');
        var resLog = zipai.res.logo_EQS;
        game_title.setTexture(resLog);
    },
    setPiaoTouch:function () {
        var self = this;
        var piao0 = self.$('PiaoNode.piaoNode.piao0');
        TouchUtil.setOnclickListener(piao0, function () {
            network.send(4101, {room_id: gameData.roomId, piao: 0});
        });
        TouchUtil.setOnclickListener(self.$('PiaoNode.piaoNode.piao2'), function () {
            network.send(4101, {room_id: gameData.roomId, piao: 2});
        });
        TouchUtil.setOnclickListener(self.$('PiaoNode.piaoNode.piao5'), function () {
            network.send(4101, {room_id: gameData.roomId, piao: 5});
        });
        TouchUtil.setOnclickListener(self.$('PiaoNode.piaoNode.piao10'), function () {
            network.send(4101, {room_id: gameData.roomId, piao: 10});
        });
    },
    /**
     * 设置其他人的手牌
     */
    setUpOtherCards: function (id) {
        var CardsNode = this.getCardsNode(id);
        if (!CardsNode)
            return;
        var SmallSize = mCard.getSmallSize();
        CardsNode.setVisible(true);
        CardsNode.removeAllChildren();
        var cardList = this.zipaiManager['CardList' + id];
        if (cardList == undefined || cardList == null) {
            return;
        }
        cc.log(cardList);
        for (var i = 0; i < cardList.length; i++) {
            var cards = cardList[i];
            for (var j = cards.length - 1; j >= 0; j--) {
                var card = new Card();
                cc.log(cards[j]);
                card.setData(cards[j], null, 2);
                if(id == 1){
                    card.setAnchorPoint(1, 1);
                    card.setPosition(i * -SmallSize.w, j * -SmallSize.h);
                }else{
                    card.setAnchorPoint(0, 1);
                    card.setPosition(i * SmallSize.w, j * -SmallSize.h);
                }
                CardsNode.addChild(card);
                card.setTouchEnableStatus(false);
                var tag = (i + 1) * 100 + j + 1;
                card.setTag(tag);
            }
        }
    },
    showOtherPai: function () {
        if (this.zipaiManager.isReplay) {
            return;
        }
        var gameOverData = this.zipaiManager.gameOverData;
        if (!gameOverData) {
            return;
        }
        var players = gameOverData['players'];
        for (var i = 0; i < players.length; i++) {
            var infoData = players[i];
            var uid = infoData['uid'];
            var row = this.zipaiManager.g_uid_to_pos[uid];
            this.updateScore(row);
            if (uid != gameData.uid) {
                this.setUpOtherCards(row);
                this.getOpenNode(row).setWeiColor(row);
            }
        }
    },
    initPlayerInfoNode: function () {
        var self = this;
        self.playersNum = this.zipaiManager.PlayersNum;
        self.PlayerInfoNode = self.$('PlayerInfo.PlayerInfoNode');
        self.PosNode = self.$('pos3', self.PlayerInfoNode);
        for (var i = 0; i < 3; i++) {
            var infoNode = self.$('info' + i, self.PlayerInfoNode);
            cc.log(infoNode.getPosition());
            infoNode.setVisible(false);
            infoNode.getChildByName('piao').setVisible(false);
            infoNode.getChildByName('txtHX').setVisible(false);
            infoNode.getChildByName('txtHX_value').setVisible(false);
            infoNode.getChildByName('timeBg').setVisible(false);
            infoNode.getChildByName('ChatInfo').setVisible(false);
            infoNode.getChildByName('ok').setVisible(false);
            infoNode.getChildByName('offline').setVisible(false);
            infoNode.getChildByName('zhuang').setVisible(false);
            infoNode.getChildByName('fangzhu').setVisible(false);


            var headf = infoNode.getChildByName('headf');
            var guangquan = playAnimScene(headf, zipai.res.PhzHead_json, headf.getContentSize().width / 2, headf.getContentSize().height / 2, true);
            guangquan.setName('guangquan');
            guangquan.setVisible(false);


            var openNode = new OpenCards();
            openNode.setName('OpenCards');
            openNode.setPos(i);
            self.$('open' + i, this.PosNode).addChild(openNode);


            var outNode = new ChuCards('out', i);
            outNode.setName('OutCards');
            self.$('outNode' + i + '.pos', this.PosNode).addChild(outNode);

            var outNode = new ChuCards('chu', i);
            outNode.setName('ChuCards');
            self.$('chuNode' + i + '.pos', this.PosNode).addChild(outNode);
        }

        this.actCard = new Card();
        this.addChild(this.actCard);
        this.showCard = new Card();
        this.PlayerInfoNode.getChildByName('showCard').addChild(this.showCard);
    },
    /**
     * @returns OpenCards
     */
    getOpenNode: function (row) {
        return this.$('open' + row, this.PosNode).getChildByName('OpenCards');
    },
    /**
     * @returns OutCards
     */
    getOutNode: function (row) {
        return this.$('outNode' + row + '.pos', this.PosNode).getChildByName('OutCards');
    },
    /**
     * @returns OutCards
     */
    getChuNode: function (row) {
        return this.$('chuNode' + row + '.pos', this.PosNode).getChildByName('ChuCards');
    },
    setRoomState: function () {
        var self = this;
        self._super();
        var isCreatedRoom = self.zipaiManager.GameStatus == self.zipaiManager.roomStatus.CREATED;
        for (var i = 0; i < self.playersNum; i++) {
            self.getOutNode(i).getParent().getParent().setVisible(!isCreatedRoom && self.zipaiManager.isAllPiao);
            self.getChuNode(i).getParent().getParent().setVisible(!isCreatedRoom && self.zipaiManager.isAllPiao);
        }
        for (; i < 3; i++) {
            self.getOutNode(i).getParent().getParent().setVisible(false);
            self.getChuNode(i).getParent().getParent().setVisible(false);
        }
    },
    /**
     * 牌归属
     * 4006
     * @param data
     */
    onCardAscription: function (data) {
        var self = this;
        var uid = data['uid'];
        var paiId = data['paiId'];
        var fromUid = data['fromUid'];
        var isMoPai = data['isMoPai'];
        if (uid == 0) {//弃牌
            var userPos = self.zipaiManager.g_uid_to_pos[fromUid];
            if (isMoPai) {
                var out = this.getOutNode(userPos);
                out.addCard(paiId, true, this.showCard);
            } else {
                var chu = this.getChuNode(userPos);
                chu.addCard(paiId, true, this.showCard);
            }
        } else {
            var row = self.zipaiManager.g_uid_to_pos[uid];
            if (row == 0) {
                this.zipaiManager.addCard(paiId);
                if (this.movecard && this.positionBak && (this.isBeginTouch)) {
                    this.moveCardBack();
                } else {
                    this.setUpCards();
                }
            }
        }
        this.showCard.setVisible(false);
        if (self.zipaiManager.isReplay) {
            this.hideReplayAction();
        }
    },
    /**
     * 设置所有玩家弃牌
     */
    setAllOutCards: function () {
        for (var i = 0; i < this.playersNum; i++) {
            var outCards = this.getOutNode(i);
            outCards.removeAllChildren();
            var uid = this.zipaiManager.g_pos_to_uid[i];
            var DiscardCards = this.zipaiManager.g_uid_to_DiscardCards[uid];
            if (DiscardCards) {
                for (var j = 0; j < DiscardCards.length; j++) {
                    var card = DiscardCards[j];
                    outCards.addCard(card);
                }
            }
            var chuCards = this.getChuNode(i);
            chuCards.removeAllChildren();
            var ChuCards = this.zipaiManager.g_uid_to_ChuCards[uid];
            if (ChuCards) {
                for (var j = 0; j < ChuCards.length; j++) {
                    var card = ChuCards[j];
                    chuCards.addCard(card);
                }
            }
        }
    },
    /**
     * 重置房间信息
     */
    clearRoom: function (roundNotAdd) {
        this._super(roundNotAdd);
        for (var i = 0; i < this.playersNum; i++) {
            this.getChuNode(i).removeAllChildren();
        }
    },
    /**
     * 重连设置飘
     */
    setUpPiao:function () {
        var self = this;
        for (var i = 0; i < this.playersNum; i++) {
            var uid = this.zipaiManager.g_pos_to_uid[i];
            var piao = this.zipaiManager.g_uid_to_Piao[uid];
            var infoNode = self.$('info' + i, self.PlayerInfoNode);
            var piaoUi = infoNode.getChildByName('piao');
            piaoUi.setVisible(piao>=0 && self.zipaiManager.withPiao);
            if (piao > 0) {
                piaoUi.setString('充'+piao+'分');
            } else {
                piaoUi.setString('不充');
            }
        }
    },
    isFreeze:function (freezeCard) {
        if(this.Freeze){
            var allFreeze = true;
            var nCards0 = this.getCardsNode(0);
            var cardSprites = nCards0.getChildren();
            for (var i = 0; i < cardSprites.length; i++) {
                cc.log(this.Freeze.indexOf(cardSprites[i].data));
                if(this.Freeze.indexOf(cardSprites[i].data)<0){
                    allFreeze = false;
                    break;
                }
            }
            if(allFreeze){
                return false
            }
            for (i = 0; i < this.Freeze.length; i++) {
                if (this.Freeze[i] == freezeCard) {
                    return true;
                }
            }
        }
        return false;
    },
    /**
     * 初始化界面
     */
    initializeUI:function () {
        this._super();
        var self = this;
        this.PiaoNode = self.$('PiaoNode');
        this.PiaoNode.setVisible(false);
    },
    setAllPai4Replay: function (data) {
        data = deepCopy(data);
        mAction.initGame();
        for (var uid in data)
            if (data.hasOwnProperty(uid)) {
                var playercard = data[uid];
                var pos = this.zipaiManager.g_uid_to_pos[uid];
                if(cc.isUndefined(pos)){
                    mAction.leftCardCount = data['uid'];
                    this.zipaiManager.setLeftCardCount(mAction.leftCardCount);
                    this.setLeftCard();
                    this.hideReplayAction();
                    continue;
                }
                var outCards = this.getOutNode(pos);
                var openCards = this.getOpenNode(pos);
                var chuCards = this.getChuNode(pos);
                if (pos == 0) {
                    this.zipaiManager.setSeverCardList(playercard['pai_arr']);
                    this.zipaiManager.setCardList();
                    this.setUpCards();
                } else {
                    this.zipaiManager.setOtherCardList(playercard['pai_arr'], pos);
                    this.setUpOtherCards(pos);
                }
                outCards.removeAllChildren();
                for (var j = 0; j < playercard['usedMoPaiArr'].length; j++) {
                    var card = playercard['usedMoPaiArr'][j];
                    outCards.addCard(card);
                }
                chuCards.removeAllChildren();
                for (j = 0; j < playercard['usedChuPaiArr'].length; j++) {
                    var card = playercard['usedChuPaiArr'][j];
                    chuCards.addCard(card);
                }
                openCards.removeAllChildren();
                for (j = 0; j < playercard['dui_arr'].length; j++) {
                    var cards =  playercard['dui_arr'][j]['pai_arr'];
                    var typ = mCard.getComboType(cards, false);
                    openCards.addCards(cards, typ);
                }
                //设置胡息
                this.setUpHX(pos);
            }
    }
});